package com.xbjtech.controller;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.xbjtech.model.StoreUser;
import com.xbjtech.model.StoreUserExample;
import com.xbjtech.service.StoreUserService;
import com.xbjtech.util.MapUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

@Controller
public class PorderController {

	@Autowired 
	StoreUserService storeUserService;
	
	@RequestMapping("/porder")
	public String pOrder() {
		return "map/porder";
	}
	
	@RequestMapping(value="/porderyb",produces="application/json;charset=utf-8")
	@ResponseBody
	public String  pOrderYB(String address) {
		StoreUserExample storeUserExample=new StoreUserExample();
		storeUserExample.createCriteria().andDeletedEqualTo(0);
		List<StoreUser> storeList = storeUserService.selectStoreList(storeUserExample);
		String coordinate = MapUtil.coordinate(address);
		String coordinate1=coordinate.replace("\"", "");
		System.out.println(coordinate1);
		Iterator<StoreUser> iterator = storeList.iterator();
		System.out.println(iterator);
		while(iterator.hasNext()) {
			StoreUser next = iterator.next();
			String destination=next.getLongitude()+","+next.getLatitude();
			System.out.println(destination);
			String distance = MapUtil.distance(coordinate1, destination);
			distance=distance.replace("\"", "");
			next.setDistance(distance);																					
		}
		Collections.sort(storeList);
		List<StoreUser> storeList2=new ArrayList<StoreUser>();
		storeList2.add(storeList.get(0));
		storeList2.add(storeList.get(1));
		storeList2.add(storeList.get(2));
		ObjectMapper mapper=new ObjectMapper();
		String storeListJson=null;
		try {
			storeListJson = mapper.writeValueAsString(storeList2);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
		System.out.println(storeListJson);
		return storeListJson;
	}
	
}






